import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/AppSaveInfoUtil.dart';
import 'package:flutter_yres/tool/LoadingUtil.dart';
import 'package:flutter_yres/tool/Constant.dart';
import 'package:flutter_yres/pages/user/vip_order/OrderComment.dart';
import 'package:flutter_yres/pages/user/vip_order/OrderPay.dart';

class OrderDetailPage extends StatefulWidget {
  String id;
  OrderDetailPage({Key key, @required this.id}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return OrderDetailView();
  }
}

class OrderDetailView extends State<OrderDetailPage> {
  String token = "",
      orderNum = "",
      order_id = "",
      title = "",
      status = "1",
      address = "",
      create_time = "",
      service_time = "",
      phone = "",
      discount_price = "",
      pay_price = "",
      type = "1",
      payment_method = "",
      service_finish_time = "",
      remark="";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
      getDetail();
    });
  }

  getDetail() async {
    FormData formData = new FormData.fromMap({"token": token, "id": widget.id});
    Response<String> response =
        await HttpUtil().post(Api.VipOrderMsgUrl, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    if (code == 0 && mounted) {
      setState(() {
        orderNum = userMap['data']['content']['order_id'];
        title = userMap['data']['content']['service_name'];
        status = userMap['data']['content']['status'];
        address = userMap['data']['content']['address'];
        phone = userMap['data']['content']['phone'];
        pay_price = userMap['data']['content']['pay_price'];
        discount_price = userMap['data']['content']['discount_price'];
        type = userMap['data']['content']['type'];
        order_id = userMap['data']['content']['id'];
        remark = userMap['data']['content']['remark'];

        service_finish_time = userMap['data']['content']['service_finish_time'];

        payment_method = userMap['data']['content']['payment_method'];
        create_time = userMap['data']['content']['create_time'];
        service_time = userMap['data']['content']['start_time']
                .toString()
                .replaceAll(":00", "") +
            " - " +
            userMap['data']['content']['end_time']
                .toString()
                .split(" ")[1]
                .replaceAll(":00", "");
      });
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "订单详情",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.5,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      body: Container(
        color: Colors.white,
        width: double.infinity,
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(15.0),
              child: Text(
                title,
                style: TextStyle(
                    color: Colors.black,
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: Divider(
                color: Colors.grey,
                height: 1,
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                  10,
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '订单编号',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      orderNum,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '订单状态',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      status == "1"
                          ? "待付款"
                          : status == "2"
                              ? "待服务"
                              : status == "3"
                                  ? "待评价"
                                  : status == "6" ? "已取消" : "已完成",
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: mainColor),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '下单时间',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      create_time,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '订单类型',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      type == "1" ? "在线支付" : "当面支付",
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '服务时间',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      service_time,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Offstage(
              offstage: type != "1",
              child: Container(
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil.getInstance().setHeight(DP_30),
                     ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_30),
                    0),
                child: new Row(
                  children: <Widget>[
                    Expanded(
                      child: Text(
                        '支付方式',
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black54),
                      ),
                      flex: 2,
                    ),
                    Expanded(
                      child: Text(
                        payment_method == null
                            ? ""
                            : payment_method == "1" ? "支付宝方式" : "微信支付",
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black),
                      ),
                      flex: 5,
                    )
                  ],
                ),
              ),
            ),
            Offstage(
              offstage: status != "3" && status != "4",
              child: Container(
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil.getInstance().setHeight(DP_30),
                     ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_30),
                    0),
                child: new Row(
                  children: <Widget>[
                    Expanded(
                      child: Text(
                        '确认时间',
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black54),
                      ),
                      flex: 2,
                    ),
                    Expanded(
                      child: Text(
                        service_finish_time == null ? "" : service_finish_time,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black),
                      ),
                      flex: 5,
                    )
                  ],
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '服务地址',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      address,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '联系电话',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      phone,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                   ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '备注信息',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      remark==null?"":remark,
                      maxLines: 4,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              color: divideColor,
              height: 10,
              width: double.infinity,
              margin: EdgeInsets.fromLTRB(0,  ScreenUtil.getInstance().setHeight(DP_20), 0, 0),
              child: Text(''),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                  10,
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '优惠金额',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      discount_price,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black87),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_30),
                  10,
                  ScreenUtil.getInstance().setHeight(DP_30),
                  0),
              child: new Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      '实际支付',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                          color: Colors.black54),
                    ),
                    flex: 2,
                  ),
                  Expanded(
                    child: Text(
                      "¥" + pay_price,
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                          color: mainColor),
                    ),
                    flex: 5,
                  )
                ],
              ),
            ),
            Expanded(
              child: Container(
                alignment: Alignment.topRight,
                margin: EdgeInsets.fromLTRB(0, 15, 0, 0),
                color: divideColor,
                padding: EdgeInsets.all(15.0),
                width: double.infinity,
                child: new Row(
                  children: <Widget>[
                    Expanded(
                      child: Text(''),
                    ),
                    Offstage(
                      offstage: status != "1",
                      child: new GestureDetector(
                        onTap: () {
                          showDialog(
                            context: context,
                            builder: (context) => new AlertDialog(
                              title: new Text('提示'),
                              content: new Text('确定取消该订单吗，取消后不可撤回'),
                              actions: <Widget>[
                                new FlatButton(
                                  onPressed: () =>
                                      Navigator.of(context).pop(false),
                                  child: new Text(
                                    '取消',
                                    style: TextStyle(color: Colors.grey),
                                  ),
                                ),
                                new FlatButton(
                                  onPressed: () {
                                    Navigator.of(context).pop(false);
                                    showDialog(
                                        context: context,
                                        barrierDismissible: false,
                                        builder: (_) {
                                          return new NetLoadingDialog(
                                            loadingText: "取消中...",
                                            bgColor:
                                                Color.fromARGB(255, 0, 0, 0),
                                            requestCallBack: cancle(),
                                            outsideDismiss: false,
                                          );
                                        });
                                  },
                                  child: new Text('确定取消',
                                      style: TextStyle(color: mainColor)),
                                ),
                              ],
                            ),
                          );
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                          decoration: new BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(
                                  color: Colors.black54, width: 1.0)),
                          child: Text(
                            '取消订单',
                            style: TextStyle(
                                fontSize: 14.0, color: Colors.black54),
                          ),
                        ),
                      ),
                    ),
                    Offstage(
                      offstage: type != "2",
                      child: new GestureDetector(
                        onTap: () {
                          showDialog(
                            context: context,
                            builder: (context) => new AlertDialog(
                              title: new Text('提示'),
                              content: new Text('确定取消该订单吗，取消后不可撤回'),
                              actions: <Widget>[
                                new FlatButton(
                                  onPressed: () =>
                                      Navigator.of(context).pop(false),
                                  child: new Text(
                                    '取消',
                                    style: TextStyle(color: Colors.grey),
                                  ),
                                ),
                                new FlatButton(
                                  onPressed: () {
                                    Navigator.of(context).pop(false);
                                    showDialog(
                                        context: context,
                                        barrierDismissible: false,
                                        builder: (_) {
                                          return new NetLoadingDialog(
                                            loadingText: "取消中...",
                                            bgColor:
                                                Color.fromARGB(255, 0, 0, 0),
                                            requestCallBack: cancle(),
                                            outsideDismiss: false,
                                          );
                                        });
                                  },
                                  child: new Text('确定取消',
                                      style: TextStyle(color: mainColor)),
                                ),
                              ],
                            ),
                          );
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                          decoration: new BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(
                                  color: Colors.black54, width: 1.0)),
                          child: Text(
                            '取消订单',
                            style: TextStyle(
                                fontSize: 14.0, color: Colors.black54),
                          ),
                        ),
                      ),
                    ),
                    Offstage(
                      offstage: status != "6" && status != "4",
                      child: new GestureDetector(
                        onTap: (){
                          del();
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                          decoration: new BoxDecoration(
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(
                                  color: Colors.black54, width: 1.0)),
                          child: Text(
                            '删除订单',
                            style: TextStyle(
                                fontSize: 14.0, color: Colors.black54),
                          ),
                        ),
                      ),
                    ),
                    Offstage(
                      offstage: status != "2",
                      child: new GestureDetector(
                        onTap: () {
                          showDialog(
                            context: context,
                            builder: (context) => new AlertDialog(
                              title: new Text('确认提示'),
                              content: new Text('确定商家已提供服务吗，确认后不可撤回?'),
                              actions: <Widget>[
                                new FlatButton(
                                  onPressed: () =>
                                      Navigator.of(context).pop(false),
                                  child: new Text(
                                    '取消',
                                    style: TextStyle(color: Colors.grey),
                                  ),
                                ),
                                new FlatButton(
                                  onPressed: () {
                                    Navigator.of(context).pop(false);
                                    showDialog(
                                        context: context,
                                        barrierDismissible: false,
                                        builder: (_) {
                                          return new NetLoadingDialog(
                                            loadingText: "确认中...",
                                            bgColor:
                                                Color.fromARGB(255, 0, 0, 0),
                                            requestCallBack: confirm(),
                                            outsideDismiss: false,
                                          );
                                        });
                                  },
                                  child: new Text('确定完成',
                                      style: TextStyle(color: mainColor)),
                                ),
                              ],
                            ),
                          );
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2,
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              0,
                              0,
                              0),
                          decoration: new BoxDecoration(
                              color: mainColor,
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(color: mainColor, width: 1.0)),
                          child: Text(
                            '确认完成',
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE),
                                color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                    Offstage(
                      offstage: status != "3",
                      child: new GestureDetector(
                        onTap: () {
                          Navigator.of(context)
                              .push<int>(MaterialPageRoute(builder: (context) {
                            return new OrderCommentPage(
                              id: order_id,
                            );
                          }));
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2,
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              0,
                              0,
                              0),
                          decoration: new BoxDecoration(
                              color: mainColor,
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(color: mainColor, width: 1.0)),
                          child: Text(
                            '评价服务',
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE),
                                color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                    Offstage(
                      offstage: status != "1",
                      child: new GestureDetector(
                        onTap: () {
                          Navigator.of(context).push(
                            new MaterialPageRoute(
                                builder: (context) => new OrderPayPage(
                                      orderId: order_id,
                                      orderNum: orderNum,
                                      totalMoney: pay_price,
                                    )),
                          );
                        },
                        child: new Container(
                          padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                          margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                          decoration: new BoxDecoration(
                              color: mainColor,
                              borderRadius: BorderRadius.circular(10.0),
                              border: Border.all(color: mainColor, width: 1.0)),
                          child: Text(
                            '去支付',
                            style:
                                TextStyle(fontSize: 14.0, color: Colors.white),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  confirm() async {
    // FormData formData = new FormData.fromMap({
    //   "token": token,
    //   "id": order_id,
    // });
    // Response<String> response =
    //     await HttpUtil().post(Api.FINISH_SERVICE, data: formData);
    // Map userMap = json.decode(response.data);
    // int code = userMap["code"];
    // String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    // if (code == 0) {
    //   getDetail();
    // }
    // return true;
  }
 del() async {
    // FormData formData = new FormData.fromMap({
    //   "token": token,
    //   "id": order_id,
    // });
    // Response<String> response =
    //     await HttpUtil().post(Api.DEL_ORDER, data: formData);
    // Map userMap = json.decode(response.data);
    // int code = userMap["code"];
    // String msg = userMap["msg"];
    // Fluttertoast.showToast(msg: msg);
    // if (code == 0) {
    //   Navigator.of(context).pop();
    // }
    // return true;
  }
  cancle() async {
    FormData formData = new FormData.fromMap({
      "token": token,
      "order_id": order_id,
    });
    Response<String> response =
        await HttpUtil().post(Api.VipOrderCancelMsgUrl, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 0) {
      getDetail();
    }
    return true;
  }
}
